package com.model;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Perpustakaan {

    public static String NAMA = "Algio Library";
    public static ArrayList<Publikasi> DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
    public static ArrayList<Anggota> DAFTAR_ANGGOTA = new ArrayList<Anggota>();
    public static ArrayList<Pinjaman> DAFTAR_PINJAMAN = new ArrayList<Pinjaman>();
    private TimerBacaPublikasi TIMER_BACA_PUBLIKASI;
    private TimerSimpanPublikasi TIMER_SIMPAN_PUBLIKASI;

    public static void tambahTabelPublikasi(Publikasi publikasi) {
        java.sql.Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314003", "MHS115314003");
        Statement statement;
        try {
            statement = conn.createStatement();
            String sql = "insert into publikasi (id_koleksi, judul) values ('"+publikasi.getIdKoleksi() + "','" + publikasi.getJudul()+"')" ;
        statement.executeUpdate(sql);
        conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }
    
    public static void tambahPublikasi(Publikasi publikasi) {
        DAFTAR_PUBLIKASI.add(publikasi);
    }

    public static void tambahAnggota(Anggota anggota) {
        DAFTAR_ANGGOTA.add(anggota);
    }

    public static String bacaDaftarPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                hasil += DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
            }
            return hasil;
        }
        return "Kosong";
    }

    public static String bacaDaftarAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                hasil += DAFTAR_ANGGOTA.get(i).getNama() + "\n";
            }
            return hasil;
        }
        return "Kosong";
    }
    
    public static void tambahPinjaman(Pinjaman pinjaman) {
        DAFTAR_PINJAMAN.add(pinjaman);
    }

    public static void simpanFilePublikasi() {
        FileOutputStream outStream = null;
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            System.out.println("Daftar Publikasi Kosong");
           
        } else {
            try {
                File outFile = new File("publikasi.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(DAFTAR_PUBLIKASI);
                outStream.close();

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                        Logger.getLogger(Anggota.class.getName()).log(Level.SEVERE, null,ex);
            }
        }
    }
    
    public static void simpanFileAnggota() {
        FileOutputStream outStream = null;
        if (DAFTAR_ANGGOTA.isEmpty()) {
            System.out.println("Daftar Anggota Kosong");
           
        } else {
            try {
                File outFile = new File("anggota.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(DAFTAR_ANGGOTA);
                outStream.close();

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                        Logger.getLogger(Anggota.class.getName()).log(Level.SEVERE, null,ex);
            }
        }
    }
    
    public  static void bacaFileAnggota(){
        try {
            FileInputStream inStream = null;
            File inFile = new File("anggota.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = new ObjectInputStream(inStream);
            DAFTAR_ANGGOTA=(ArrayList<Anggota>)inObjectStream.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    public static void SimpanTabelPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null,"Daftar Publikasi Kosong"); }
        else {
            try {
                Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314003", "MHS115314003") ;
                        Statement statement = conn.createStatement();
                        for(int i=0;i<DAFTAR_PUBLIKASI.size();i++){
                        String sql = "insert into publikasi (id_koleksi, judul) "
                                + "values ('"+DAFTAR_PUBLIKASI.get(i).getIdKoleksi() 
                                + "','" + DAFTAR_PUBLIKASI.get(i).getJudul()+"')";
                        statement.executeUpdate(sql);
                        }
                        
                        conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public  static void bacaFilePublikasi(){
        try {
            FileInputStream inStream = null;
            File inFile = new File("publikasi.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = new ObjectInputStream(inStream);
            DAFTAR_PUBLIKASI=(ArrayList<Publikasi>)inObjectStream.readObject();
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void bacaTabelPublikasi() {
        DAFTAR_PUBLIKASI= new ArrayList<Publikasi>();
        java.sql.Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314003", "MHS115314003");
        Statement statement;
        try {
            statement = conn.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        String sql = "select * from publikasi";
        ResultSet result = null;
        try {
            statement = conn.createStatement();
            result = statement.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            if (result.next()) {
                
            }
            else {
            
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void aturTimer() {
        
    }
    
    public static ArrayList<Anggota> cariAnggota(String nama) {
        ArrayList<Anggota> hasil = new ArrayList<Anggota>();
        for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
            Anggota temp = DAFTAR_ANGGOTA.get(i);
            if(temp.getNama().contains(nama))
                hasil.add(temp);
        }
        return hasil;
        
    }
        } 